/*====================================================================================
                                                                     
		Financial Constraints and Propagation of Shocks in Production Networks
			    By Demir-Javorcik-Michalski-Ors
  
*====================================================================================*/	

clear all
set more off
log close _all
set matsize 5000

cd "\data\" // Add your directory here


/*====================================================================================
                                                                     
		Financial Constraints and Propagation of Shocks in Production Networks
			    By Demir-Javorcik-Michalski-Ors
  
*====================================================================================*/	

clear all
set more off
log close _all
set matsize 5000

cd "\data\" // Add your directory here

//Load imports data
use "ithalat.dta",clear

rename sırano ID
rename sırano_donem year
rename kýymet imports

keep if year==2010

drop if regimecode==4051 | regimecode==4100 | regimecode==4171 | regimecode==4123 | regimecode==4191 | regimecode==4251 | regimecode==5100 | regimecode==5123 | regimecode==5141 | regimecode==5171 | regimecode==5191 | regimecode==5200 | regimecode==5221 | regimecode==5223 | regimecode==5271 | regimecode==5291 | regimecode==5341 | regimecode==5351 

gen HS6=floor(HS12/1000000)
collapse (sum) imports,by(ID country_code HS6)
bys ID ,sort: egen totalimports=sum(imports)
gen import_share=imports/totalimports

sort ID
merge ID using temp1.dta
drop _merge

sort ID
merge ID using temp2.dta
drop _merge

sort HS6 country_code
merge  HS6 country_code using "tempvarietyexp" // This dataset has to be transferred from TSI
keep if _m==3
drop _merge

egen v=group(HS6 country_code)

bys v,sort: gen numf=_N

bys v,sort: egen imports_v=sum(imports)
gen share_fv=imports/imports_v

gsort v -share_fv
by v: gen rank=_n

save tempcheck.dta,replace


/***************************************
/Table II.9
****************************************/
use tempcheck,clear

collapse (mean) numf Exposure ,by( country_code HS6 )
replace Exposure=0 if Exposure==.

su numf,det
su numf if Exposure>0,det

use tempcheck,clear

su share_fv if numf>1 & rank==1,det
gen top2=rank<3
bys v top2,sort: egen share_top2=sum(share_fv)
collapse (mean) share_top2 numf,by(v top2)
su share_top2 if numf>3 & top2==1,det


/***************************************
/Table II.5
****************************************/
 
 //Identify the most exposed HS2 codes
use tempcheck,clear

gen hs2=floor(HS6/10000)
collapse (mean) Exposure,by(hs2)
drop if Exposure==.

gsort -Exposure
gen rank=_n
keep if rank<21

keep hs2
sort hs2
save tophs2,replace

use "ithalat.dta",clear

rename sırano ID
rename sırano_donem year
rename kýymet imports

sort ulke
merge ulke using "TUIK-country codes.dta" 
drop if _merge==2
drop if country_code==""
drop if country_code=="TUR"
drop _merge ulke

keep if year==2010

drop if regimecode==4051 | regimecode==4100 | regimecode==4171 | regimecode==4123 | regimecode==4191 | regimecode==4251 | regimecode==5100 | regimecode==5123 | regimecode==5141 | regimecode==5171 | regimecode==5191 | regimecode==5200 | regimecode==5221 | regimecode==5223 | regimecode==5271 | regimecode==5291 | regimecode==5341 | regimecode==5351 

gen HS6=floor(HS12/1000000)
collapse (sum) imports,by(ID country_code HS6)
bys ID ,sort: egen totalimports=sum(imports)
gen import_share=imports/totalimports

sort ID
merge ID using temp1.dta
drop _merge

sort ID
merge ID using temp2.dta
drop _merge

gen hs2=floor(HS6/10000)
sort hs2
merge hs2 using tophs2.dta
drop if _m==3 // drop the most exposed HS2 codes
drop _merge

sort HS6 country_code
merge  HS6 country_code using "tempvarietyexp" // This dataset has to be transferred from TSI
drop _merge

gen FirmExposure_hs2=(1.5*totalimports/(wage0+DomPurch0+1.5*totalimports))*import_share*VarietyExposure

collapse (sum) FirmExposure_hs2 ,by(ID)

sort ID
save tophs2.dta,replace


//Identify the most exposed countries
use tempcheck,clear

bys country_code,sort: gen nump=_N
drop if nump<50 // drop source countries with small number of products

collapse (mean) Exposure,by(country_code)
drop if Exposure==.

gsort -Exposure

gen rank=_n
keep if rank<21

keep country_code
sort country_code
save topc,replace

use "ithalat.dta",clear

rename sırano ID
rename sırano_donem year
rename kýymet imports

sort ulke
merge ulke using "TUIK-country codes.dta" 
drop if _merge==2
drop if country_code==""
drop if country_code=="TUR"
drop _merge ulke

keep if year==2010

drop if regimecode==4051 | regimecode==4100 | regimecode==4171 | regimecode==4123 | regimecode==4191 | regimecode==4251 | regimecode==5100 | regimecode==5123 | regimecode==5141 | regimecode==5171 | regimecode==5191 | regimecode==5200 | regimecode==5221 | regimecode==5223 | regimecode==5271 | regimecode==5291 | regimecode==5341 | regimecode==5351 

gen HS6=floor(HS12/1000000)
collapse (sum) imports,by(ID country_code HS6)
bys ID ,sort: egen totalimports=sum(imports)
gen import_share=imports/totalimports

sort ID
merge ID using temp1.dta
drop _merge

sort ID
merge ID using temp2.dta
drop _merge

sort country_code
merge country_code using topc.dta
drop if _m==3 // drop the most exposed source countries
drop _merge

sort HS6 country_code
merge  HS6 country_code using "tempvarietyexp" // This dataset has to be transferred from TSI
drop _merge

gen FirmExposure_c=(1.5*totalimports/(wage0+DomPurch0+1.5*totalimports))*import_share*VarietyExposure

collapse (sum) FirmExposure_c ,by(ID)

sort ID
save topc.dta,replace


//Identify the most imprtant hs2 codes
use "ithalat.dta",clear

rename sırano ID
rename sırano_donem year
rename kýymet imports

sort ulke
merge ulke using "TUIK-country codes.dta" 
drop if _merge==2
drop if country_code==""
drop if country_code=="TUR"
drop _merge ulke

keep if year==2010

drop if regimecode==4051 | regimecode==4100 | regimecode==4171 | regimecode==4123 | regimecode==4191 | regimecode==4251 | regimecode==5100 | regimecode==5123 | regimecode==5141 | regimecode==5171 | regimecode==5191 | regimecode==5200 | regimecode==5221 | regimecode==5223 | regimecode==5271 | regimecode==5291 | regimecode==5341 | regimecode==5351 

gen HS6=floor(HS12/1000000)
gen hs2=floor(HS12/10000000000)

collapse (sum) imports,by(hs2)
egen total=sum(imports)
gen share=imports/total

gsort -share

gen rank=_n
keep if rank<6

keep hs2
sort hs2
save tophs2total,replace


use "ithalat.dta",clear

rename sırano ID
rename sırano_donem year
rename kýymet imports

sort ulke
merge ulke using "TUIK-country codes.dta" 
drop if _merge==2
drop if country_code==""
drop if country_code=="TUR"
drop _merge ulke

keep if year==2010

drop if regimecode==4051 | regimecode==4100 | regimecode==4171 | regimecode==4123 | regimecode==4191 | regimecode==4251 | regimecode==5100 | regimecode==5123 | regimecode==5141 | regimecode==5171 | regimecode==5191 | regimecode==5200 | regimecode==5221 | regimecode==5223 | regimecode==5271 | regimecode==5291 | regimecode==5341 | regimecode==5351 

gen HS6=floor(HS12/1000000)
collapse (sum) imports,by(ID country_code HS6)
bys ID ,sort: egen totalimports=sum(imports)
gen import_share=imports/totalimports

sort ID
merge ID using temp1.dta
drop _merge

sort ID
merge ID using temp2.dta
drop _merge

gen hs2=floor(HS12/10000000000)
sort hs2
merge hs2 using tophs2total.dta
drop if _m==3 // drop the most important hs2 codes
drop _merge

sort HS6 country_code
merge  HS6 country_code using "tempvarietyexp" // This dataset has to be transferred from TSI
drop _merge

gen FirmExposure_hs2total=(1.5*totalimports/(wage0+DomPurch0+1.5*totalimports))*import_share*VarietyExposure

collapse (sum) FirmExposure_hs2total ,by(ID)

sort ID
save tophs2total.dta,replace


//Identify the most imprtant source countries
use "ithalat.dta",clear

rename sırano ID
rename sırano_donem year
rename kýymet imports

sort ulke
merge ulke using "TUIK-country codes.dta" 
drop if _merge==2
drop if country_code==""
drop if country_code=="TUR"
drop _merge ulke

keep if year==2010

drop if regimecode==4051 | regimecode==4100 | regimecode==4171 | regimecode==4123 | regimecode==4191 | regimecode==4251 | regimecode==5100 | regimecode==5123 | regimecode==5141 | regimecode==5171 | regimecode==5191 | regimecode==5200 | regimecode==5221 | regimecode==5223 | regimecode==5271 | regimecode==5291 | regimecode==5341 | regimecode==5351 

gen HS6=floor(HS12/1000000)
gen hs2=floor(HS12/10000000000)

collapse (sum) imports,by(country_code)
egen total=sum(imports)
gen share=imports/total

gsort -share

gen rank=_n
keep if rank<6

keep country_code
sort country_code
save topcountrytotal,replace


use "ithalat.dta",clear

rename sırano ID
rename sırano_donem year
rename kýymet imports

sort ulke
merge ulke using "TUIK-country codes.dta" 
drop if _merge==2
drop if country_code==""
drop if country_code=="TUR"
drop _merge ulke

keep if year==2010

drop if regimecode==4051 | regimecode==4100 | regimecode==4171 | regimecode==4123 | regimecode==4191 | regimecode==4251 | regimecode==5100 | regimecode==5123 | regimecode==5141 | regimecode==5171 | regimecode==5191 | regimecode==5200 | regimecode==5221 | regimecode==5223 | regimecode==5271 | regimecode==5291 | regimecode==5341 | regimecode==5351 

gen HS6=floor(HS12/1000000)
collapse (sum) imports,by(ID country_code HS6)
bys ID ,sort: egen totalimports=sum(imports)
gen import_share=imports/totalimports

sort ID
merge ID using temp1.dta
drop _merge

sort ID
merge ID using temp2.dta
drop _merge

sort country_code
merge country_code using topcountrytotal.dta
drop if _m==3 // drop the most important source countries
drop _merge

sort HS6 country_code
merge  HS6 country_code using "tempvarietyexp" // This dataset has to be transferred from TSI
drop _merge

gen FirmExposure_countrytotal=(1.5*totalimports/(wage0+DomPurch0+1.5*totalimports))*import_share*VarietyExposure

collapse (sum) FirmExposure_countrytotal ,by(ID)

sort ID
save topcountrytotal.dta,replace


use temp.dta,clear

keep if year==2012

sort ID
merge ID using tophs2.dta
drop _m

sort ID
merge ID using topc.dta
drop _m

sort ID
merge ID using tophs2total.dta
drop _m

sort ID
merge ID using topcountrytotal.dta
drop _m

for X in any FirmExposure_hs2 FirmExposure_c FirmExposure_countrytotal FirmExposure_hs2total:  replace X=0 if X==. & ImpInt0==0
for X in any hs2 c countrytotal hs2total: gen Chgtau_X=FirmExposure_X*(ln(1+0.06)-ln(1+0.03))


 foreach var of varlist Chgtau_hs2 Chgtau_c Chgtau_hs2total Chgtau_countrytotal  {
 reghdfe ChgSales `var'  ImpInt0 lnEmp0  ,absorb(sr) vce(cluster sr)
estimates store `var'
estadd local "FE" "sr" , replace
}

noi esttab Chgtau_hs2 Chgtau_c Chgtau_hs2total Chgtau_countrytotal , compress width(1\hsize) ///
 se star(* 0.10 ** 0.05 *** 0.01) s(N r2 FE clustvar)

 
 
/***************************************
/Table II.10
****************************************/

use tempcheck,clear

gen omega=(1.5*totalimports/(wage0+DomPurch0+1.5*totalimports))*import_share

bys firm_ID,sort: gen numf=_n==1
replace numf=sum(numf)
replace numf=numf[_N]

collapse (sum) omega (mean) numf VarietyExposure,by(country_code HS6)
gen x_v=omega/numf 


su x_v,det
